#ifndef THINK_THINK_MODEL_RELATION_HASMANY_H
#define THINK_THINK_MODEL_RELATION_HASMANY_H

static char* think_model_relation_hasmany =
	"\n"
	"// +----------------------------------------------------------------------\n"
	"// | ThinkPHP [ WE CAN DO IT JUST THINK ]\n"
	"// +----------------------------------------------------------------------\n"
	"// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.\n"
	"// +----------------------------------------------------------------------\n"
	"// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )\n"
	"// +----------------------------------------------------------------------\n"
	"// | Author: liu21st <liu21st@gmail.com>\n"
	"// +----------------------------------------------------------------------\n"
	"\n"
	"namespace think\\model\\relation;\n"
	"\n"
	"use think\\db\\Query;\n"
	"use think\\Loader;\n"
	"use think\\Model;\n"
	"use think\\model\\Relation;\n"
	"\n"
	"class HasMany extends Relation\n"
	"{\n"
	"    /**\n"
	"     * 构造函数\n"
	"     * @access public\n"
	"     * @param Model  $parent     上级模型对象\n"
	"     * @param string $model      模型名\n"
	"     * @param string $foreignKey 关联外键\n"
	"     * @param string $localKey   当前模型主键\n"
	"     */\n"
	"    public function __construct(Model $parent, $model, $foreignKey, $localKey)\n"
	"    {\n"
	"        $this->parent     = $parent;\n"
	"        $this->model      = $model;\n"
	"        $this->foreignKey = $foreignKey;\n"
	"        $this->localKey   = $localKey;\n"
	"        $this->query      = (new $model)->db();\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 延迟获取关联数据\n"
	"     * @param string   $subRelation 子关联名\n"
	"     * @param \\Closure $closure     闭包查询条件\n"
	"     * @return false|\\PDOStatement|string|\\think\\Collection\n"
	"     */\n"
	"    public function getRelation($subRelation = \'\', $closure = null)\n"
	"    {\n"
	"        if ($closure) {\n"
	"            call_user_func_array($closure, [ & $this->query]);\n"
	"        }\n"
	"        $list   = $this->relation($subRelation)->select();\n"
	"        $parent = clone $this->parent;\n"
	"\n"
	"        foreach ($list as &$model) {\n"
	"            $model->setParent($parent);\n"
	"        }\n"
	"\n"
	"        return $list;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 预载入关联查询\n"
	"     * @access   public\n"
	"     * @param array    $resultSet   数据集\n"
	"     * @param string   $relation    当前关联名\n"
	"     * @param string   $subRelation 子关联名\n"
	"     * @param \\Closure $closure     闭包\n"
	"     * @return void\n"
	"     */\n"
	"    public function eagerlyResultSet(&$resultSet, $relation, $subRelation, $closure)\n"
	"    {\n"
	"        $localKey = $this->localKey;\n"
	"        $range    = [];\n"
	"        foreach ($resultSet as $result) {\n"
	"            // 获取关联外键列表\n"
	"            if (isset($result->$localKey)) {\n"
	"                $range[] = $result->$localKey;\n"
	"            }\n"
	"        }\n"
	"\n"
	"        if (!empty($range)) {\n"
	"            $data = $this->eagerlyOneToMany($this->query, [\n"
	"                $this->foreignKey => [\n"
	"                    \'in\',\n"
	"                    $range,\n"
	"                ],\n"
	"            ], $relation, $subRelation, $closure);\n"
	"            // 关联属性名\n"
	"            $attr = Loader::parseName($relation);\n"
	"            // 关联数据封装\n"
	"            foreach ($resultSet as $result) {\n"
	"                if (!isset($data[$result->$localKey])) {\n"
	"                    $data[$result->$localKey] = [];\n"
	"                }\n"
	"\n"
	"                foreach ($data[$result->$localKey] as &$relationModel) {\n"
	"                    $relationModel->setParent(clone $result);\n"
	"                }\n"
	"\n"
	"                $result->setRelation($attr, $this->resultSetBuild($data[$result->$localKey]));\n"
	"            }\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 预载入关联查询\n"
	"     * @access   public\n"
	"     * @param Model    $result      数据对象\n"
	"     * @param string   $relation    当前关联名\n"
	"     * @param string   $subRelation 子关联名\n"
	"     * @param \\Closure $closure     闭包\n"
	"     * @return void\n"
	"     */\n"
	"    public function eagerlyResult(&$result, $relation, $subRelation, $closure)\n"
	"    {\n"
	"        $localKey = $this->localKey;\n"
	"\n"
	"        if (isset($result->$localKey)) {\n"
	"            $data = $this->eagerlyOneToMany($this->query, [$this->foreignKey => $result->$localKey], $relation, $subRelation, $closure);\n"
	"            // 关联数据封装\n"
	"            if (!isset($data[$result->$localKey])) {\n"
	"                $data[$result->$localKey] = [];\n"
	"            }\n"
	"\n"
	"            foreach ($data[$result->$localKey] as &$relationModel) {\n"
	"                $relationModel->setParent(clone $result);\n"
	"            }\n"
	"\n"
	"            $result->setRelation(Loader::parseName($relation), $this->resultSetBuild($data[$result->$localKey]));\n"
	"        }\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 关联统计\n"
	"     * @access public\n"
	"     * @param Model    $result  数据对象\n"
	"     * @param \\Closure $closure 闭包\n"
	"     * @return integer\n"
	"     */\n"
	"    public function relationCount($result, $closure)\n"
	"    {\n"
	"        $localKey = $this->localKey;\n"
	"        $count    = 0;\n"
	"        if (isset($result->$localKey)) {\n"
	"            if ($closure) {\n"
	"                call_user_func_array($closure, [ & $this->query]);\n"
	"            }\n"
	"            $count = $this->query->where($this->foreignKey, $result->$localKey)->count();\n"
	"        }\n"
	"        return $count;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 创建关联统计子查询\n"
	"     * @access public\n"
	"     * @param \\Closure $closure 闭包\n"
	"     * @param string   $name    统计数据别名\n"
	"     * @return string\n"
	"     */\n"
	"    public function getRelationCountQuery($closure, &$name = null)\n"
	"    {\n"
	"        if ($closure) {\n"
	"            $return = call_user_func_array($closure, [ & $this->query]);\n"
	"            if ($return && is_string($return)) {\n"
	"                $name = $return;\n"
	"            }\n"
	"        }\n"
	"        $localKey = $this->localKey ?: $this->parent->getPk();\n"
	"        return $this->query->whereExp($this->foreignKey, \'=\' . $this->parent->getTable() . \'.\' . $localKey)->fetchSql()->count();\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 一对多 关联模型预查询\n"
	"     * @access public\n"
	"     * @param object $model       关联模型对象\n"
	"     * @param array  $where       关联预查询条件\n"
	"     * @param string $relation    关联名\n"
	"     * @param string $subRelation 子关联\n"
	"     * @param bool   $closure\n"
	"     * @return array\n"
	"     */\n"
	"    protected function eagerlyOneToMany($model, $where, $relation, $subRelation = \'\', $closure = false)\n"
	"    {\n"
	"        $foreignKey = $this->foreignKey;\n"
	"        // 预载入关联查询 支持嵌套预载入\n"
	"        if ($closure) {\n"
	"            call_user_func_array($closure, [ & $model]);\n"
	"        }\n"
	"        $list = $model->removeWhereField($foreignKey)->where($where)->with($subRelation)->select();\n"
	"\n"
	"        // 组装模型数据\n"
	"        $data = [];\n"
	"        foreach ($list as $set) {\n"
	"            $data[$set->$foreignKey][] = $set;\n"
	"        }\n"
	"        return $data;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 保存（新增）当前关联数据对象\n"
	"     * @access public\n"
	"     * @param mixed $data 数据 可以使用数组 关联模型对象 和 关联对象的主键\n"
	"     * @return Model|false\n"
	"     */\n"
	"    public function save($data)\n"
	"    {\n"
	"        if ($data instanceof Model) {\n"
	"            $data = $data->getData();\n"
	"        }\n"
	"\n"
	"        // 保存关联表数据\n"
	"        $data[$this->foreignKey] = $this->parent->{$this->localKey};\n"
	"\n"
	"        $model = new $this->model();\n"
	"        return $model->save($data) ? $model : false;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 创建关联对象实例\n"
	"     * @param array $data\n"
	"     * @return Model\n"
	"     */\n"
	"    public function make($data = [])\n"
	"    {\n"
	"        if ($data instanceof Model) {\n"
	"            $data = $data->getData();\n"
	"        }\n"
	"\n"
	"        // 保存关联表数据\n"
	"        $data[$this->foreignKey] = $this->parent->{$this->localKey};\n"
	"\n"
	"        return new $this->model($data);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 批量保存当前关联数据对象\n"
	"     * @access public\n"
	"     * @param array $dataSet 数据集\n"
	"     * @return integer\n"
	"     */\n"
	"    public function saveAll(array $dataSet)\n"
	"    {\n"
	"        $result = false;\n"
	"        foreach ($dataSet as $key => $data) {\n"
	"            $result = $this->save($data);\n"
	"        }\n"
	"        return $result;\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 根据关联条件查询当前模型\n"
	"     * @access public\n"
	"     * @param string  $operator 比较操作符\n"
	"     * @param integer $count    个数\n"
	"     * @param string  $id       关联表的统计字段\n"
	"     * @param string  $joinType JOIN类型\n"
	"     * @return Query\n"
	"     */\n"
	"    public function has($operator = \'>=\', $count = 1, $id = \'*\', $joinType = \'INNER\')\n"
	"    {\n"
	"        $table    = $this->query->getTable();\n"
	"        $model    = basename(str_replace(\'\\\\\', \'/\', get_class($this->parent)));\n"
	"        $relation = basename(str_replace(\'\\\\\', \'/\', $this->model));\n"
	"\n"
	"        return $this->parent->db()\n"
	"            ->alias($model)\n"
	"            ->field($model . \'.*\')\n"
	"            ->join([$table => $relation], $model . \'.\' . $this->localKey . \'=\' . $relation . \'.\' . $this->foreignKey, $joinType)\n"
	"            ->group($relation . \'.\' . $this->foreignKey)\n"
	"            ->having(\'count(\' . $id . \')\' . $operator . $count);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 根据关联条件查询当前模型\n"
	"     * @access public\n"
	"     * @param  mixed     $where  查询条件（数组或者闭包）\n"
	"     * @param  mixed     $fields 字段\n"
	"     * @return Query\n"
	"     */\n"
	"    public function hasWhere($where = [], $fields = null)\n"
	"    {\n"
	"        $table    = $this->query->getTable();\n"
	"        $model    = basename(str_replace(\'\\\\\', \'/\', get_class($this->parent)));\n"
	"        $relation = basename(str_replace(\'\\\\\', \'/\', $this->model));\n"
	"\n"
	"        if (is_array($where)) {\n"
	"            foreach ($where as $key => $val) {\n"
	"                if (false === strpos($key, \'.\')) {\n"
	"                    $where[$relation . \'.\' . $key] = $val;\n"
	"                    unset($where[$key]);\n"
	"                }\n"
	"            }\n"
	"        }\n"
	"\n"
	"        $fields = $this->getRelationQueryFields($fields, $model);\n"
	"\n"
	"        return $this->parent->db()->alias($model)\n"
	"            ->field($fields)\n"
	"            ->group($model . \'.\' . $this->localKey)\n"
	"            ->join([$table => $relation], $model . \'.\' . $this->localKey . \'=\' . $relation . \'.\' . $this->foreignKey)\n"
	"            ->where($where);\n"
	"    }\n"
	"\n"
	"    /**\n"
	"     * 执行基础查询（进执行一次）\n"
	"     * @access protected\n"
	"     * @return void\n"
	"     */\n"
	"    protected function baseQuery()\n"
	"    {\n"
	"        if (empty($this->baseQuery)) {\n"
	"            if (isset($this->parent->{$this->localKey})) {\n"
	"                // 关联查询带入关联条件\n"
	"                $this->query->where($this->foreignKey, $this->parent->{$this->localKey});\n"
	"            }\n"
	"            $this->baseQuery = true;\n"
	"        }\n"
	"    }\n"
	"\n"
	"}\n"
	"\n"
;
#endif